package com.kuaimacode.kframework.api.controller.user;

import com.google.common.collect.Lists;
import com.kuaimacode.kframework.api.controller.BaseController;
import com.kuaimacode.kframework.api.service.CertificationService;
import com.kuaimacode.kframework.api.service.UploadService;
import com.kuaimacode.kframework.api.vo.request.TwoElementsReq;
import com.kuaimacode.kframework.api.vo.response.CertifyTokenResp;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.exception.ErrorCode;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.api.controller.user
 * @Description: created by IntelliJ IDEA
 * @date 2019-03-17 20:53
 */
@RestController
@RequestMapping("certificate")
@Api(tags = "实名认证", description = "实名认证相关api")
public class CertificationController extends BaseController {

    @Autowired
    private CertificationService certificationService;
    @Autowired
    private UploadService uploadService;

    private static final List<String> ID_CARD_LOCATION = Lists.newArrayList("pos", "neg");

    @ApiOperation(value = "获取实名认证token", notes = "获取实名认证token")
    @GetMapping("/getToken")
    public JsonResult<CertifyTokenResp> getToken() {
        return JsonResult.success(certificationService.getToken());
    }

    @ApiOperation(value = "上传身份证照片", notes = "上传身份证照片")
    @PostMapping("/uploadIdCard")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片数据", required = true, dataType = "string"),
            @ApiImplicitParam(name = "location", value = "pos代表正面照，neg代表反面照", required = true, dataType = "string")
    })
    public JsonResult uploadIdCard(@RequestParam("file") MultipartFile file, @RequestParam("location") String location) {
        if (!ID_CARD_LOCATION.contains(location)) {
            throw new ServerInternalErrorException(ErrorCode.PARAMETER_ILLEGAL_ERROR + "", "参数错误");
        }
        return JsonResult.success(uploadService.uploadIdCard(file, location, getUserId()));
    }

    @ApiOperation(value = "上传头部照片", notes = "上传头部照片")
    @PostMapping("/uploadCertiHead")
    @ApiImplicitParam(name = "file", value = "图片数据", required = true, dataType = "string")
    public JsonResult uploadCertiHead(@RequestParam("file") MultipartFile file) {
        return JsonResult.success(uploadService.uploadCertiHead(file, getUserId()));
    }

    @ApiOperation(value = "实名认证上传完身份证照片，填完姓名身份证号后下一步接口", notes = "实名认证上传完身份证照片，填完姓名身份证号后下一步接口")
    @PostMapping("/saveTwoElements")
    @ApiImplicitParam(name = "req", value = "TwoElementsReq实体", required = true, dataType = "TwoElementsReq")
    public JsonResult<Boolean> saveTwoElements(@RequestBody @Valid TwoElementsReq twoElementsReq) {
        return JsonResult.success(certificationService.saveTwoElements(twoElementsReq, getUserId()));
    }

    @ApiOperation(value = "实名认证上传完头部照片，下一步保存头部照片接口", notes = "实名认证上传完头部照片，下一步保存头部照片接口")
    @PostMapping("/saveCertiHead")
    @ApiImplicitParam(name = "headImgName", value = "头部照片名称", required = true, dataType = "string")
    public JsonResult<Boolean> saveCertiHead(@RequestBody Map<String, String> params) {
        Optional.ofNullable(params.get("headImgName")).orElseThrow(() -> new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR));
        return JsonResult.success(certificationService.saveCertiHead(params.get("headImgName"), getUserId()));
    }

    @ApiOperation(value = "查询认证状态", notes = "查询认证状态")
    @PostMapping("/getStatus")
    @ApiImplicitParam(name = "ticketId", value = "获取token接口返回的ticketId", required = true, dataType = "string")
    public JsonResult<Boolean> getStatus(@RequestParam String ticketId) {
        return JsonResult.success(certificationService.getStatus(ticketId, getUserId()));
    }

    @ApiOperation(value = "纯服务端认证", notes = "纯服务端认证")
    @GetMapping("/rPMinCertify")
    public JsonResult rPMinCertify() {
        certificationService.rPMinCertify(getUserId());
        return JsonResult.success();
    }
}
